/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.huangp.domain;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import org.hibernate.annotations.Index;



/**
 *
 * @author Patrick Huang
 */
@Entity
@Table(name="sms")
public class SMS implements Serializable {
    //--------------------------------------------------------------------------
    // Static Properties
    //--------------------------------------------------------------------------
    public static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss");
    public static final String DELIMITER = "|";
    //--------------------------------------------------------------------------
    // Properties
    //--------------------------------------------------------------------------
    @Id
    @Column(name="sms_id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Column
    private String sender;

    @Column
    private String receiver;

    @Column(length=255)
    @Index(name="message_index")
    private String message;

    @Column(name="sms_time",updatable=false)
    @Temporal(javax.persistence.TemporalType.TIME)
    private Date smsTime;

    @Column(name="sms_date",updatable=false)
    @Temporal(javax.persistence.TemporalType.DATE)
    @Index(name="sms_date_index")
    private Date smsDate;
    // -------------------------------------------------------------------------
    // Implementation Methods
    // -------------------------------------------------------------------------
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("SMS[\n");
        sb.append("id:[").append(id==null?"":id).append("],\n")
            .append("smsDate:[").append(smsDate).append("],\n")
            .append("smsTime:[").append(smsTime).append("],\n")
            .append("sender:[").append(sender).append("],")
            .append("receiver:[").append(receiver).append("],\n")
            .append("message:[").append(message).append("]\n]")
            ;
        return sb.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final SMS other = (SMS) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if ((this.message == null) ? (other.message != null) : !this.message.equals(other.message)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 67 * hash + (this.smsTime != null ? this.smsTime.hashCode() : 0);
        return hash;
    }

    /**
     * helper method. Combines date and time together and produce string
     * @return string represents sms date and time
     */
    public String getSmsDateTime(){
        if(this.smsDate == null || this.smsTime == null){
            return "";
        }
        StringBuilder dateTime = new StringBuilder();
        dateTime.append(dateFormatter.format(smsDate))
                .append(" ")
                .append(timeFormatter.format(smsTime));
        return dateTime.toString();
    }

    /**
     * helper method. To set smsDate and smsTime by one single entry
     * @param dateTime
     * @throws ParseException
     */
    public void setSmsDateTime(Date dateTime) throws ParseException{
        this.smsDate = dateFormatter.parse(dateFormatter.format(dateTime));
        this.smsTime = timeFormatter.parse(timeFormatter.format(dateTime));
    }

    /**
     * helper method. Concatenate field values into a CSV string
     * @return csv string represents current field values
     */
    public String toDelimiteredString(){
        StringBuilder sb = new StringBuilder();
        sb.append(id!=null?id:"").append(DELIMITER)
            .append(sender).append(DELIMITER)
            .append(receiver).append(DELIMITER)
            .append(message).append(DELIMITER)
            .append(smsDate).append(DELIMITER)
            .append(smsTime)
                ;
        return sb.toString();
    }
    // -------------------------------------------------------------------------
    // Mutator Methods
    // -------------------------------------------------------------------------
    public Date getSmsTime() {
        return smsTime;
    }

    public void setSmsTime(Date smsTime) {
        this.smsTime = smsTime;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getSmsDate() {
        return smsDate;
    }

    public void setSmsDate(Date smsDate) {
        this.smsDate = smsDate;
    }   
}
